﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.MountAndBlade.View.BannerVisual
// Assembly: TaleWorlds.MountAndBlade.View, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 61152EB8-513B-4D4A-8AC0-0D1465C6CF6C
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\Modules\Native\bin\Win64_Shipping_Client\TaleWorlds.MountAndBlade.View.dll

using System;
using TaleWorlds.Core;
using TaleWorlds.DotNet;
using TaleWorlds.Engine;
using TaleWorlds.Library;
using TaleWorlds.MountAndBlade.View.Tableaus;

#nullable disable
namespace TaleWorlds.MountAndBlade.View
{
  public class BannerVisual : IBannerVisual
  {
    public Banner Banner { get; private set; }

    public BannerVisual(Banner banner) => this.Banner = banner;

    public void ValidateCreateTableauTextures()
    {
    }

    public Texture GetTableauTextureSmall(Action<Texture> setAction, bool isTableauOrNineGrid = true)
    {
      return TableauCacheManager.Current.BeginCreateBannerTexture(BannerCode.CreateFrom(this.Banner), setAction, isTableauOrNineGrid);
    }

    public Texture GetTableauTextureLarge(Action<Texture> setAction, bool isTableauOrNineGrid = true)
    {
      return TableauCacheManager.Current.BeginCreateBannerTexture(BannerCode.CreateFrom(this.Banner), setAction, isTableauOrNineGrid, true);
    }

    public static MatrixFrame GetMeshMatrix(
      ref Mesh mesh,
      float marginLeft,
      float marginTop,
      float width,
      float height,
      bool mirrored,
      float rotation,
      float deltaZ)
    {
      MatrixFrame identity = MatrixFrame.Identity;
      double num1 = (double) width / 1528.0;
      float num2 = height / 1528f;
      double boundingBoxWidth = (double) mesh.GetBoundingBoxWidth();
      float x = (float) (num1 / boundingBoxWidth);
      float y = num2 / mesh.GetBoundingBoxHeight();
      identity.rotation.RotateAboutUp(rotation);
      if (mirrored)
        identity.rotation.RotateAboutForward(3.14159274f);
      identity.rotation.ApplyScaleLocal(new Vec3(x, y, 1f));
      identity.origin.x = 0.0f;
      identity.origin.y = 0.0f;
      identity.origin.x += marginLeft / 1528f;
      identity.origin.y -= marginTop / 1528f;
      identity.origin.z += deltaZ;
      return identity;
    }

    public MetaMesh ConvertToMultiMesh()
    {
      BannerData bannerData1 = this.Banner.BannerDataList[0];
      MetaMesh metaMesh = MetaMesh.CreateMetaMesh();
      Mesh fromResource1 = Mesh.GetFromResource(BannerManager.Instance.GetBackgroundMeshName(bannerData1.MeshId));
      Mesh copy = fromResource1.CreateCopy();
      fromResource1.ManualInvalidate();
      copy.Color = BannerManager.GetColor(bannerData1.ColorId2);
      copy.Color2 = BannerManager.GetColor(bannerData1.ColorId);
      MatrixFrame meshMatrix1 = BannerVisual.GetMeshMatrix(ref copy, bannerData1.Position.x, bannerData1.Position.y, bannerData1.Size.x, bannerData1.Size.y, bannerData1.Mirror, (float) ((double) bannerData1.RotationValue * 2.0 * 3.1415927410125732), 0.5f);
      copy.SetLocalFrame(meshMatrix1);
      metaMesh.AddMesh(copy);
      copy.ManualInvalidate();
      for (int index = 1; index < this.Banner.BannerDataList.Count; ++index)
      {
        BannerData bannerData2 = this.Banner.BannerDataList[index];
        BannerIconData iconDataFromIconId = BannerManager.Instance.GetIconDataFromIconId(bannerData2.MeshId);
        Material fromResource2 = Material.GetFromResource(iconDataFromIconId.MaterialName);
        if ((NativeObject) fromResource2 != (NativeObject) null)
        {
          Mesh meshWithMaterial = Mesh.CreateMeshWithMaterial(fromResource2);
          float a = (float) (iconDataFromIconId.TextureIndex % 4) * 0.25f;
          float b = (float) (1.0 - (double) (iconDataFromIconId.TextureIndex / 4) * 0.25);
          Vec2 uvCoord1 = new Vec2(a, b);
          Vec2 uvCoord2 = new Vec2(a + 0.25f, b - 0.25f);
          UIntPtr num1 = meshWithMaterial.LockEditDataWrite();
          int num2 = meshWithMaterial.AddFaceCorner(new Vec3(-0.5f, -0.5f), new Vec3(z: 1f), uvCoord1 + new Vec2(0.0f, -0.25f), uint.MaxValue, num1);
          int patchNode1_1 = meshWithMaterial.AddFaceCorner(new Vec3(0.5f, -0.5f), new Vec3(z: 1f), uvCoord2, uint.MaxValue, num1);
          int num3 = meshWithMaterial.AddFaceCorner(new Vec3(0.5f, 0.5f), new Vec3(z: 1f), uvCoord1 + new Vec2(0.25f, 0.0f), uint.MaxValue, num1);
          int patchNode1_2 = meshWithMaterial.AddFaceCorner(new Vec3(-0.5f, 0.5f), new Vec3(z: 1f), uvCoord1, uint.MaxValue, num1);
          meshWithMaterial.AddFace(num2, patchNode1_1, num3, num1);
          meshWithMaterial.AddFace(num3, patchNode1_2, num2, num1);
          meshWithMaterial.UnlockEditDataWrite(num1);
          meshWithMaterial.SetColorAndStroke(BannerManager.GetColor(bannerData2.ColorId), BannerManager.GetColor(bannerData2.ColorId2), bannerData2.DrawStroke);
          MatrixFrame meshMatrix2 = BannerVisual.GetMeshMatrix(ref meshWithMaterial, bannerData2.Position.x, bannerData2.Position.y, bannerData2.Size.x, bannerData2.Size.y, bannerData2.Mirror, (float) ((double) bannerData2.RotationValue * 2.0 * 3.1415927410125732), (float) index);
          meshWithMaterial.SetLocalFrame(meshMatrix2);
          metaMesh.AddMesh(meshWithMaterial);
        }
      }
      return metaMesh;
    }
  }
}
